<?php

/**
 *  This script returns a JSON string containing the popular tags ordered 
 *  by reference count. Response is limited to 10 tags.
 *  
 *  INPUT:
 *  Doesn't need any query parameter, only the session data are required, 
 *  according to which the currently logged in user can be determined.
 *  
 *  OUTPUT:
 *  JSON string representation of an 2 element associative array with keys "sent" and "received". 
 *  Each element of the "sent" array and the "received" element has the following fields:
 *  <li>tid			[integer]	- ID of the tag 
 *  <li>name        [string]	- Name of the tag
 *  
 *  Note: the [string] format means JSON's string format, so including the white and special char formattings,
 *  e.g. '/' -> '\/', '\' -> '\\', ...etc. See http://www.json.org/ for the full specification.
 */
header('Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');

try {
    include_once './log4php/Logger.php';
    include_once './Situare.php';
    include_once './I18N.php';
    include_once './Error.php';
    include_once './Utils.php';
    include_once './constants.php';

    // initialize logging
    Logger::configure('./log4j.properties');

    $log = Logger::getLogger(basename(__FILE__));
    $log->debug('[START] ' . basename(__FILE__));

    $START_TIME = microtime(TRUE);

    // read the input params
    $inputArray = Utils::getInputParameters();
    $api = Utils::getInputParameterValue($inputArray, PARAM_API, NULL);

    $situare = new Situare();    
    
    // in case of invalid session
    if (!$situare->isValidSession()) {
        $log->debug('$_COOKIE[] = ' . Utils::toString($_COOKIE));
        $log->debug('$_POST[] = ' . Utils::toString($_POST));
        $log->debug('$_GET[] = ' . Utils::toString($_GET));
        throw new SituareException(
                "Not found valid session data!",
                ERROR_SESSION_INVALID_OR_MISSING);
    }

	// get the user tags
	$popularTagsArray = $situare->getPopularTags();

    // build up the result array containing user tags
    $resultArray = array();
    $resultArray['popular_tags'] = $popularTagsArray;

    // create JSON string from the result array and return it
    $jsonStr = json_encode($resultArray);
    
    if ($api == API_2_0) {
        $response = Situare::getApi20Response($resultArray);
        echo $response;
        $log->debug('Result JSON string = ' . $response);
    }
    else {
        echo $jsonStr;
        $log->debug('Result JSON string = ' . $jsonStr);
    }
    
    $log->debug('[ END ] ' . basename(__FILE__));

    $END_TIME = microtime(TRUE);
    $log->debug('[PERF ] Total script execution took ' . ($END_TIME - $START_TIME) . "ms");
} catch (Exception $e) {
    $log->error("Exception:");
    $log->error("Code: " . $e->getCode());
    $log->error("Message: " . $e->getMessage());
    $log->error("Location: " . $e->getFile() . " at line " . $e->getLine());
    $log->error("Trace: " . $e->getTraceAsString());

    if ($api == API_2_0) {
        echo Situare::getApi20Response($e);
    }
    else  {
        echo getExceptionAsJson($e);
    }
}



?>
